package cs342.project1.mathNotebook.gui;

import cs342.project1.mathNotebook.data.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.io.*;
import java.lang.*;
import java.sql.*;
import java.util.*;

public class PermissionsScreen extends JFrame {
	
	private final int width = 700, height = 500;
	private final int X_PADDING = 25;
	private final int Y_PADDING = 50;
	
	private final int TABLE_X = 25;
	private final int TABLE_Y = 25;
	private final int TABLE_WIDTH = width - 2 * TABLE_X;
	private final int TABLE_HEIGHT = 350;	
	
	private JButton acceptButton;
	private JTable table;
	private JScrollPane pane;
	private PermissionsTable pTable;
	
	private int fileID;
	private int size;
	private String mode;
	
	public PermissionsScreen(int fileID, String mode){
		this.setSize(width, height);
        this.setTitle("Set Permissions");
        this.setLocationRelativeTo(null);
        
        this.mode = mode;
        
        this.fileID = fileID;
        size = 0;
        
        acceptButton = new JButton("Accept");
        acceptButton.setSize(100,30);
        acceptButton.setLocation(width - acceptButton.getWidth() - X_PADDING, height - acceptButton.getHeight() - Y_PADDING);
        this.add(acceptButton);
        
        this.setLayout(null);
        
        this.setResizable(true);
        this.setVisible(true);
        initialize();
	}
	
	public void initialize(){
		// Get data from database
		Object[][] data = new Object[1][1];
	
		data = getData();
		
		// Create the tablemodel
		pTable = new PermissionsTable(data);
		// Create the table
		table = new JTable(pTable);
		table.setSize(TABLE_WIDTH, TABLE_HEIGHT);
		table.setLocation(TABLE_X, TABLE_Y);
		table.setVisible(true);
		// Add the table
		this.add(table);
		// Add the table listener
		
		pane = new JScrollPane(table);
		pane.setSize(TABLE_WIDTH,TABLE_HEIGHT);
		pane.setLocation(TABLE_X,TABLE_Y);
		pane.setVisible(true);
		this.add(pane);
		
		this.repaint();
	}
	
	private Object[][] getData() {
		
		if(mode.equals("set")){
			try{
				MathDatabase mdb = new MathDatabase();
				mdb.open();
				mdb.getStatement().executeQuery("SELECT userID FROM Users");
				ResultSet rs = mdb.getStatement().getResultSet();
				
				int row = 0;
				int col = 1;
				
				int userID;
				String username;
				
				rs.first();
				size = 1;
				while(!rs.isLast()){
					size++;
					rs.next();
				}
				
				Object[][] data = new Object[size][3];
				
				rs.first();
				
				while(!rs.isLast()){
					// Get the username
					userID = rs.getInt(col);
					username = getUsername(userID);
					data[row][col-1] = username;
					col++;
					// Read-Write
					data[row][col-1] = false;
					col++;
					// Read Only
					data[row][col-1] = false;
	
					rs.next();
					row++;
					col = 1;
				}
				
				// Get the username
				userID = rs.getInt(col);
				username = getUsername(userID);
				data[row][col-1] = username;
				col++;
				// Read-Write
				data[row][col-1] = false;
				col++;
				// Read Only
				data[row][col-1] = false;
				
				mdb.close();
				return data;
			}
			catch (Exception e){
				System.out.println(e);
				e.printStackTrace();
			}
		}
		else if(mode.equals("modify")){
			try{
				MathDatabase mdb = new MathDatabase();
				mdb.open();
				mdb.getStatement().executeQuery("SELECT userID, Permission FROM Permissions WHERE fileID =" + fileID);
				ResultSet rs = mdb.getStatement().getResultSet();
			
				int row = 0;
				int col = 0;
				
				int userID;
				String username;
				int permission_type;
				
				rs.first();
				size = 1;
				while(!rs.isLast()){
					size++;
					rs.next();
				}
				
				Object[][] data = new Object[size][3];
				
				rs.first();
				
				while(!rs.isLast()){
					
					// Get the username
					userID = rs.getInt(1);
					username = getUsername(userID);
					data[row][col] = username;
					col++;
					
					permission_type = rs.getInt(2);
					if(permission_type == 0){
						// Read-Write
						data[row][col] = true;
						col++;
						// Read Only
						data[row][col] = false;
					}
					else if(permission_type == 1){
						// Read-Write
						data[row][col] = false;
						col++;
						// Read Only
						data[row][col] = true;
					}
					else{
						// Read-Write
						data[row][col] = false;
						col++;
						// Read Only
						data[row][col] = false;
					}
	
					rs.next();
					row++;
					col = 0;
				}
				
				// Get the username
				userID = rs.getInt(1);
				username = getUsername(userID);
				data[row][col] = username;
				col++;
				
				permission_type = rs.getInt(2);
				if(permission_type == 0){
					// Read-Write
					data[row][col] = true;
					col++;
					// Read Only
					data[row][col] = false;
				}
				else if(permission_type == 1){
					// Read-Write
					data[row][col] = false;
					col++;
					// Read Only
					data[row][col] = true;
				}
				else{
					// Read-Write
					data[row][col] = false;
					col++;
					// Read Only
					data[row][col] = false;
				}
				
				mdb.close();
				return data;
			}
			catch(Exception e){
			}
		}
		
		return null;
		
	}
	
	private String getUsername(int id){
		try
		{
			
		MathDatabase mdb = new MathDatabase();
		mdb.open();
		
		mdb.getStatement().executeQuery("SELECT username FROM Users WHERE userID=" + id);
		ResultSet rs = mdb.getStatement().getResultSet();
		rs.first();
		
		String username = rs.getString(1);
		
		return username;
		
		}
		catch(Exception e){}
		return null;
	}
	
	public void setPermissions(){
		if(mode.equals("set")){
			try{
				Object[][] data = pTable.getData();
				
				for(int i = 0; i < size; i++){
					String username = (String)data[i][0];
					boolean bool_one = (Boolean)data[i][1];
					boolean bool_two = (Boolean)data[i][2];
					
					MathDatabase mdb = new MathDatabase();
					mdb.open();
					
					if(bool_one){
						int permission = 0;
						// Get the userId
						mdb.getStatement().executeQuery("SELECT userID FROM Users WHERE username=" + '"' + username + '"');
						ResultSet rs = mdb.getStatement().getResultSet();
						rs.first();
						int userID = rs.getInt(1);
						// Insert into Permissions
						PreparedStatement ps = mdb.createPreparedStatement("INSERT INTO Permissions(fileID,userID,Permission) VALUES(?,?,?)");
						
						ps.setInt(1,fileID);
						ps.setInt(2,userID);
						ps.setInt(3,permission);
						
						ps.executeUpdate();
						mdb.commit();				
					}
					else if(bool_two){
						int permission = 1;
						// Get the userId
						mdb.getStatement().executeQuery("SELECT userID FROM Users WHERE username=" + '"' + username + '"');
						ResultSet rs = mdb.getStatement().getResultSet();
						rs.first();
						int userID = rs.getInt(1);
						// Insert into Permissions
						PreparedStatement ps = mdb.createPreparedStatement("INSERT INTO Permissions(fileID,userID,Permission) VALUES(?,?,?)");
						
						ps.setInt(1,fileID);
						ps.setInt(2,userID);
						ps.setInt(3,permission);
						
						ps.executeUpdate();
						mdb.commit();
					}			
					else{
						int permission = 2;
						// Get the userId
						mdb.getStatement().executeQuery("SELECT userID FROM Users WHERE username=" + '"' + username + '"');
						ResultSet rs = mdb.getStatement().getResultSet();
						rs.first();
						int userID = rs.getInt(1);
						// Insert into Permissions
						PreparedStatement ps = mdb.createPreparedStatement("INSERT INTO Permissions(fileID,userID,Permission) VALUES(?,?,?)");
						
						ps.setInt(1,fileID);
						ps.setInt(2,userID);
						ps.setInt(3,permission);
						
						ps.executeUpdate();
						mdb.commit();
					}
					mdb.close();
				}
			}
			catch(Exception e){}
		}
		else if(mode.equals("modify")){
			try{
				Object[][] data = pTable.getData();
				
				for(int i = 0; i < size; i++){
					String username = (String)data[i][0];
					boolean bool_one = (Boolean)data[i][1];
					boolean bool_two = (Boolean)data[i][2];
					
					MathDatabase mdb = new MathDatabase();
					mdb.open();
					
					if(bool_one){
						int permission = 0;
						// Get the userId
						mdb.getStatement().executeQuery("SELECT userID FROM Users WHERE username=" + '"' + username + '"');
						ResultSet rs = mdb.getStatement().getResultSet();
						rs.first();
						int userID = rs.getInt(1);
						// Insert into Permissions
						PreparedStatement ps = mdb.createPreparedStatement("Update Permissions SET Permission = ? WHERE fileID = " + fileID);
						
						ps.setInt(1,permission);
						
						ps.executeUpdate();
						mdb.commit();				
					}
					else if(bool_two){
						int permission = 1;
						// Get the userId
						mdb.getStatement().executeQuery("SELECT userID FROM Users WHERE username=" + '"' + username + '"');
						ResultSet rs = mdb.getStatement().getResultSet();
						rs.first();
						int userID = rs.getInt(1);
						// Insert into Permissions
						PreparedStatement ps = mdb.createPreparedStatement("Update Permissions SET Permission = ? WHERE fileID = " + fileID);
						
						ps.setInt(1,permission);
						
						ps.executeUpdate();
						mdb.commit();
					}			
					else{
						int permission = 2;
						// Get the userId
						mdb.getStatement().executeQuery("SELECT userID FROM Users WHERE username=" + '"' + username + '"');
						ResultSet rs = mdb.getStatement().getResultSet();
						rs.first();
						int userID = rs.getInt(1);
						// Insert into Permissions
						PreparedStatement ps = mdb.createPreparedStatement("Update Permissions SET Permission = ? WHERE fileID = " + fileID);
						
						ps.setInt(1,permission);
						
						ps.executeUpdate();
						mdb.commit();
					}
					mdb.close();
				}
			}
			catch(Exception e){}
		}
	}
	
	public JButton getAcceptButton(){
		return acceptButton;
	}
	
}